#define WIN32_LEAN_AND_MEAN //stupid thing just for visual C++ to only use what is necessay

#include <windows.h> //for window app
#include <GL/gl.h> //need to use opengl
#include <GL/glu.h> //opengl utilities
#include <include/glaux.h> //auxiliray functions

bool fullScreen = TRUE;
float angle = 0.0f; //current angle of our test triangle
HDC g_HDC; //global device context...whatever that means lol.. it hink it just handles the window

void SetupPixelFormat( HDC hDC )
{
    int nPixelFormat; //pixel format index
static PIXELFORMATDESCRIPTOR pfd = {
sizeof( PIXELFORMATDESCRIPTOR ), //size of structure
1, //version..just always set this to 1
PFD_DRAW_TO_WINDOW | //support window
PFD_SUPPORT_OPENGL | //obviously support opengl
PFD_DOUBLEBUFFER, //double buffering is amazing so we r gonna support it
PFD_TYPE_RGBA, //this is the color mode
32, //go to 32 bit color mode
0, 0, 0, 0, 0, 0, //ignore these color bits for whatever reason
0, //no alpha buffer
0, //ignore shift bit
0, //no accumulation buffer
0, 0, 0, 0, //ignore accumulation bits
16, //16 bit z-buffer size
0, //no stencil buffer
0, //no auxiliray buffer
PFD_MAIN_PLANE, //main drawing plane
0, //reversed
0, 0, 0 }; //layer masks ignored...finally done with that structure

nPixelFormat = ChoosePixelFormat( hDC, &pfd ); //pick the best pixel format

SetPixelFormat( hDC, nPixelFormat, &pfd );

}


//now for windows proc event handler

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
static HGLRC hRC; //rendering context
static HDC hDC; //device context
int width, height; //window width and height

switch( message )
{
case WM_CREATE: //window is being created

hDC = GetDC( hwnd ); //get the current window's device context
g_HDC = hDC;
SetupPixelFormat( hDC ); //call pixel format setup function

//create rendering context and make it current
hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );

return( 0 );
break;

case WM_CLOSE: //window closing

//deselect rendering context and delete it cuz we dont need it
wglMakeCurrent( hDC, NULL );
wglDeleteContext( hRC );

//send quit message
PostQuitMessage( 0 );

return( 0 );
break;

case WM_SIZE:
height = HIWORD( lParam );//get window height and width
width = LOWORD( lParam );

if( height == 0 ) //we probably shouldnt ever divide by zero
{
height = 1;
}

//reset the viewport to new dimensions
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION ); //set projection matrix
glLoadIdentity(); //reset projection matrix

//now for the aspect ratio of the window
gluPerspective( 54.0f, ( GLfloat )width / ( GLfloat )height, 1.0f, 1000.0f );

glMatrixMode( GL_MODELVIEW ); //set modelview matrix
glLoadIdentity(); //reset modelvew matrix

return( 0 );
break;

default:
break;
}

return( DefWindowProc( hwnd, message, wParam, lParam ) );

}

//main entry point...aka main function finally

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nShowCMD )
{
WNDCLASSEX windowClass; //obviously windows class
HWND hwnd; //window handle
MSG msg; //message
bool done; //flag that says the app is done
DWORD dwExStyle; //window extended mode
DWORD dwStyle;
RECT windowRect;


int width = 800;
int height = 600;
int bits = 32;

windowRect.left = (long)0;
windowRect.right = (long)width;
windowRect.top = (long)0;
windowRect.bottom = (long)height;

//and now for the glorious window class structure
//i dunno what most of this means..but copy and paste is awesome and hopefully it works

windowClass.cbSize = sizeof( WNDCLASSEX );
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon( NULL, IDI_WINLOGO );

//register windows class that we made
if( !RegisterClassEx( &windowClass ) )

return( 0 );

//so now it registered cuz it didnt fail
//our fullscreen attempt
if( fullScreen )
{
    DEVMODE dmScreenSettings; //device mode
    memset( &dmScreenSettings, 0, sizeof( dmScreenSettings ) );
    dmScreenSettings.dmSize = sizeof( dmScreenSettings );
    dmScreenSettings.dmPelsWidth = width; //width
    dmScreenSettings.dmPelsHeight = height; //hight
    dmScreenSettings.dmBitsPerPel = bits; //bits per pixel
    dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

    if( ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN ) != DISP_CHANGE_SUCCESSFUL )
    {
        MessageBox( NULL, "Display Failed", NULL, MB_OK );
        fullScreen = FALSE;
    }
}

if( fullScreen )
{
    dwExStyle = WS_EX_APPWINDOW; //window extended style
    dwStyle = WS_POPUP; //windowed style
    ShowCursor( FALSE ); //hide mouse pointer
}
else
{
    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    dwStyle = WS_OVERLAPPEDWINDOW;
}

AdjustWindowRectEx( &windowRect, dwStyle, FALSE, dwExStyle );

//create window
hwnd = CreateWindowEx( NULL, //extended style
"MyClass", //class name
"Pyhsics-game", //app name
dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0, //x,y
windowRect.right - windowRect.left, //width
windowRect.bottom - windowRect.top, //height
NULL, //handle parent
NULL, //handle menu
hInstance, //app instance
NULL ); //no extra params

//check to see if our window failed soo see if hwnd = null
if( !hwnd )
return( 0 );

ShowWindow( hwnd, SW_SHOW ); //display the window we worked so hard to make
UpdateWindow( hwnd ); //update window

done = false; //init the loop condition var

//main message loop
while( !done )
{
PeekMessage( &msg, hwnd, NULL, NULL, PM_REMOVE );

if( msg.message == WM_QUIT ) //if u got the quit message
{
done = true; //then u better quit
}
else
{
//do the rendering here
//clear screen and depth buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); //reset modelview matrix

angle = angle + 0.1f; //increse rotation angle counter
if( angle >= 360.0f ) //reset angle counter
angle = 0.0f;
glTranslatef( 0.0f, 0.0f, -5.0f ); //move back 5 units
glRotatef( angle, 0.0f, 0.0f, 1.0f ); //we shall rotate along the z axis


glColor3f( 1.0f, 0.0f, 0.0f ); //lets use the color red
glBegin( GL_TRIANGLES ); //draw the triangle
glVertex3f( 0.0f, 0.0f, 0.0f );
glVertex3f( 1.0f, 0.0f, 0.0f ); //these are the verticies x,y,z
glVertex3f( 1.0f, 1.0f, 0.0f );
glEnd();

SwapBuffers( g_HDC ); //bring the back buffer tio the front

TranslateMessage( &msg ); //translate/dispatch to the even queue
DispatchMessage( &msg );
}

}

if( fullScreen )
{
    ChangeDisplaySettings( NULL, 0 );
    ShowCursor( TRUE );
}

return msg.wParam;

}


